package com.gsoe.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gsoe.auth.mapper.ClassMapper;
import com.gsoe.auth.po.Class;
import com.gsoe.auth.service.ClassService;

import com.gsoe.auth.vo.AllClassVO;
import com.gsoe.auth.vo.ClassVO;
import com.gsoe.base.utils.UserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 班级表 服务实现类
 * </p>
 *
 * @author xiaoma
 * @since 2024-12-14
 */
@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {
    @Autowired
    private ClassMapper classMapper;

    @Override
    public List<ClassVO> getUserClass(Long userId, String input) {
        if (userId == null) userId = UserUtil.getLoginUser().getId();
        return classMapper.getUserClass(userId, input);
    }

    @Override
    public List<AllClassVO> getAllClass(String input) {
        Long userId = UserUtil.getLoginUser().getId();
        List<Long> joinedIdList = classMapper.getUserClass(userId, "")
                .stream().map(ClassVO::getId).toList();

        List<Class> classes = lambdaQuery()
                .like(StringUtils.isNotEmpty(input), Class::getClassName, input)
                .list();

        return classes.stream().map(c -> {
            AllClassVO allClassVO = new AllClassVO();
            BeanUtils.copyProperties(c, allClassVO);
            allClassVO.setJoined(joinedIdList.contains(c.getId()));
            return allClassVO;
        }).sorted((a,b)->(a.getJoined()?b.getJoined()?0:-1:1)).toList();


    }
}
